package tw.edu.tku.im.maze.database;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import tw.edu.tku.im.maze.AddRoomRep;
import tw.edu.tku.im.maze.EnterRoomRep;
import tw.edu.tku.im.maze.EnterRoomReq;
import tw.edu.tku.im.maze.Packet;

public class EnterRoomProcessor extends PacketProcessor {

   @Override
   String acceptName() {
      return "enterRoom";
   }

   @Override
   Packet process(Packet packet) {
    EnterRoomReq req = (EnterRoomReq)packet;
    Connection con;
    try {
       con = DriverManager.getConnection(address,user,passwd);
       con.setCatalog(database);
       CallableStatement cstmt = 
             con.prepareCall("{call usp_enterRoom(?,?,?,?,?)}");
       cstmt.setInt(1, req.getRoomID());
       cstmt.setString(2, req.getUserID());
       cstmt.setInt(3, req.getTicket());
       cstmt.setString(4, req.getRoomPassword());
       cstmt.registerOutParameter(5, java.sql.Types.INTEGER);
       cstmt.execute();
       int state = cstmt.getInt(5);
       return new EnterRoomRep(state);
    } catch (SQLException e) {
       e.printStackTrace();
    }
    return new EnterRoomRep(5);
   }

}
